package com.self.campussecondhandmarket.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.campussecondhandmarket.entity.domain.Goods;
import com.self.campussecondhandmarket.entity.domain.User;
import com.self.campussecondhandmarket.entity.request.goods.*;
import com.self.campussecondhandmarket.entity.response.goods.DetailResp;
import com.self.campussecondhandmarket.entity.response.goods.GoodsResp;
import com.self.campussecondhandmarket.mapper.GoodsMapper;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.GoodsService;
import com.self.campussecondhandmarket.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 31465
 * @description 针对表【tb_goods(商品表)】的数据库操作Service实现
 * @createDate 2024-04-12 09:48:38
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
		implements GoodsService {
	
	@Autowired
	UserService userService;
	
	@Override
	public Result<String> addGoodsInformation(GoodsAddReq dto) {
		
		Goods goods = new Goods();
		BeanUtils.copyProperties(dto, goods);
		goods.setUserId(dto.getUserid());
		goods.setTypes(dto.getType());
		
		String imgJsonString = JSON.toJSONString(dto.getImg().stream().distinct().toList());
		goods.setImg(imgJsonString);
		this.baseMapper.insert(goods);
		return Result.success();
	}
	
	@Override
	public List<GoodsResp> getAllGoods(GoodsAll dto) {
		List<Goods> goods = this.baseMapper.selectList(Wrappers.<Goods>lambdaQuery()
				.eq(Goods::getUserId, dto.getUserid())
				.eq(Goods::getAction, dto.getAction())
		);
		return goods.stream().map(o -> {
			GoodsResp goodsResp = new GoodsResp();
			BeanUtils.copyProperties(o, goodsResp);
			goodsResp.setImg(JSON.parseArray(o.getImg(), String.class));
			return goodsResp;
		}).toList();
	}
	
	@Override
	public DetailResp getGoodsById(Integer goodsId) {
		Goods goods = this.baseMapper.selectOne(Wrappers.<Goods>lambdaQuery()
				.eq(Goods::getId, goodsId)
		);
		User user = userService.getOne(Wrappers.<User>lambdaQuery()
				.eq(User::getId, goods.getUserId())
		);
		DetailResp vo = new DetailResp();
		BeanUtils.copyProperties(goods, vo);
		vo.setSellName(user.getName());
		vo.setCreateTime(goods.getCreateTime());
		vo.setAvatarUrl(user.getAvatarUrl());
		vo.setId(goodsId);
		vo.setStar(user.getStar());
		if (goods.getImg() != null)
			vo.setImg(JSON.parseArray(goods.getImg(), String.class));
		
		return vo;
		
	}
	
	@Override
	public void changeInventory(Inventory dto) {
		
		
		Goods goods = this.baseMapper.selectOne(Wrappers.<Goods>lambdaQuery().eq(Goods::getId, dto.getId()));
		goods.setNum(goods.getNum() - dto.getQuantity());
		this.baseMapper.updateById(goods);
	}
	
	@Override
	public void removeWithAction(Action dto) {
		this.baseMapper.update(Wrappers.<Goods>lambdaUpdate()
				.eq(Goods::getId, dto.getId())
				.set(Goods::getAction, dto.getAction())
		);
	}
	
	@Override
	public Result getNewGoods(NewGoods dto) {
		List<Goods> goods = this.baseMapper.selectList(Wrappers.<Goods>lambdaQuery()
				.like(dto.getSearchKey() != null, Goods::getName, dto.getSearchKey())
				.orderByDesc(Goods::getCreateTime)
		);
		List<com.self.campussecondhandmarket.entity.response.goods.NewGoods> list = goods.stream().map(o -> {
			com.self.campussecondhandmarket.entity.response.goods.NewGoods vo = new com.self.campussecondhandmarket.entity.response.goods.NewGoods();
			BeanUtils.copyProperties(o, vo);
			List<String> strings = JSON.parseArray(o.getImg(), String.class);
			vo.setImgs(strings);
			return vo;
		}).toList();
		return Result.success(list);
	}
	
	@Override
	public Result<String> addGoodsInformation(AdminGoodsAdd dto) {
		Goods goods = new Goods();
		BeanUtils.copyProperties(dto, goods);
		goods.setImg("[" + dto.getImg() + "]");
		this.baseMapper.insert(goods);
		return Result.success("添加成功");
	}
}




